package com.my.app.tools.ui.awt;

import com.my.app.tools.pojo.em.EditToolType;
import com.my.app.tools.util.AppToolUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

public class ScreenShotEditWindow extends JWindow {

    private int orgx, orgy, endx, endy, width, height, screenX, screenY;
    private ScreenShotMainWindow parent;
    private BufferedImage image, tempImage;

    public ScreenShotEditWindow(ScreenShotMainWindow parent) {
        this.parent = parent;

        // 鼠标十字光标
        this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                //鼠标点击时记录起始点坐标
                orgx = e.getX();
                orgy = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isMetaDown()) {
                    parent.cleanImage();
                    parent.setVisible(false);
                    return;
                }
                endx = e.getX();
                endy = e.getY();
                tempImage = image;
            }
        });

        this.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                //鼠标拖动时，记录坐标并重绘窗口
                endx = e.getX();
                endy = e.getY();
                Image dragImage = createImage(image.getWidth(), image.getHeight());
                Rectangle drawRectangle = AppToolUtil.createRectangle(orgx, orgy, endx, endy);
                EditToolType toolType = parent.getEditToolType();
                //临时图像，用于缓冲屏幕区域防止屏幕闪烁
                Graphics2D g2d = (Graphics2D) dragImage.getGraphics();
                g2d.setColor(toolType.getColor());
                BasicStroke stroke = new BasicStroke(toolType.getWeight());
                if (toolType.isDottedLine()) {
                    stroke = new BasicStroke(toolType.getWeight(), BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 5, new float[]{5, 5}, 0);
                }
                g2d.setStroke(stroke);
                g2d.drawImage(tempImage, 0, 0, null);

                switch (toolType) {
                    case RECT_HOLLOW:
                        g2d.drawRect(drawRectangle.x, drawRectangle.y, drawRectangle.width, drawRectangle.height);
                        break;
                    case RECT_FILL:
                        g2d.fillRect(drawRectangle.x, drawRectangle.y, drawRectangle.width, drawRectangle.height);
                        break;
                    case OVAL_HOLLOW:
                        g2d.drawOval(drawRectangle.x, drawRectangle.y, drawRectangle.width, drawRectangle.height);
                        break;
                    case OVAL_FILL:
                        g2d.fillOval(drawRectangle.x, drawRectangle.y, drawRectangle.width, drawRectangle.height);
                        break;
                    case LINE:
                        g2d.drawLine(orgx, orgy, endx, endy);
                        break;
                    case ARROW:
                        g2d.drawLine(orgx, orgy, endx, endy);
                        Point[] points = AppToolUtil.calculateArrowOtherPoint(orgx, orgy, endx, endy, 15);
                        Polygon p = new Polygon();
                        p.addPoint(endx, endy);
                        p.addPoint(points[0].x, points[0].y);
                        p.addPoint(points[1].x, points[1].y);
                        p.addPoint(endx, endy);
                        g2d.fillPolygon(p);
                        break;
                    default:
                        break;
                }
                ScreenShotEditWindow.this.getGraphics().drawImage(dragImage, 0, 0, ScreenShotEditWindow.this);
                image = (BufferedImage) dragImage;
                g2d.dispose();
            }
        });

    }

    public void showImage(BufferedImage image, int x, int y) {
        this.image = image;
        this.tempImage = image;
        this.setBounds(x, y, image.getWidth(), image.getHeight());
    }

    @Override
    public void paint(Graphics g) {
        if (tempImage != null) {
            g.drawImage(tempImage, 0, 0, ScreenShotEditWindow.this);
        }
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
